package com.example.administrator.rxjavarxandroid_retrofit.mvp.view;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.rxjavarxandroid_retrofit.R;
import com.example.administrator.rxjavarxandroid_retrofit.base.BaseActivity;
import com.example.administrator.rxjavarxandroid_retrofit.http.api.CommonApi;
import com.example.administrator.rxjavarxandroid_retrofit.mvp.contract.MainContract;
import com.example.administrator.rxjavarxandroid_retrofit.mvp.presenter.MainPresenter;
import com.example.administrator.rxjavarxandroid_retrofit.pojo.TableIp;
import com.example.administrator.rxjavarxandroid_retrofit.reject.component.AppComponent;
import com.example.administrator.rxjavarxandroid_retrofit.reject.component.DaggerActivityComponent;
import com.example.administrator.rxjavarxandroid_retrofit.reject.moudle.ActivityModule;
import com.example.administrator.rxjavarxandroid_retrofit.util.ToastUtil;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.ConfirmPassword;
import com.mobsandgeeks.saripaar.annotation.Email;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Password;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.MainView, Validator.ValidationListener {


    //先从module中查找是否提供需要的类实例，不存在再从@inject注解的构造函数去获取对象实例

    //通过appmodule类提供CommonApi的实例对象
    @Inject
    CommonApi mApi;

    //通过@inject注解构造函数提供实例对象
    @Inject
    ToastUtil mToastUtil;

    private Validator mValidator;

    @NotEmpty
    @Email(message = "输入邮箱地址无效")
    @Bind(R.id.mTv1)
    EditText mTv1;

    @Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS)
    @Bind(R.id.mTv2)
    EditText mTv2;

    @ConfirmPassword
    @Bind(R.id.mTv3)
    EditText mTv3;

    @Override
    protected int setLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent, ActivityModule activityModule) {
        //DaggerActivityComponent.builder().activityModule(activityModule).appComponent(appComponent).build().inject(this)然后Activity中所有被@Inject标记的类，都会通过ActivityComponent来进行初始化；
        DaggerActivityComponent.builder().activityModule(activityModule).appComponent(appComponent).build().inject(this);
    }

    @Override
    protected void initEventAndData() {
        mValidator = new Validator(this);
        mValidator.setValidationListener(this);
    }

    @Override
    public void getIpAddressSuccess(TableIp tableIp) {
        mToastUtil.showToastShort(mContext, tableIp.getItems().get(0).getProvince());
    }

    @Override
    public void onValidationSucceeded() {
        Toast.makeText(this, "Yay! we got it right!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onValidationFailed(List<ValidationError> errors) {
        for (ValidationError error : errors) {
            View view = error.getView();
            String message = error.getCollatedErrorMessage(this);

            // Display error messages ;)
            if (view instanceof EditText) {
                ((EditText) view).setError(message);
            } else {
                Toast.makeText(this, message, Toast.LENGTH_LONG).show();
            }
        }
    }

    @OnClick(R.id.email_sign_in_button)
    public void onClick() {
        mValidator.validate();
    }
}
